/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JP_Cliente.java
 *
 * Created on Jul 21, 2009, 4:23:50 PM
 */

package gui;
import delegate.GestionDelegate;
import controlacciones.ejb3.entity.TbCliente;
import controlacciones.ejb3.entity.TbDatosCliente;
import helper.JP_Utiles_PanelBotonesMantenimiento;
import helper.JP_Utiles_PanelModelo;
import gui.tabbedpane.JP_Masivo;
import helper.Utiles_MensajesDialogo;
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Task;

/**
 *
 * 
 */
public class JP_ClienteMasivo extends JP_Utiles_PanelModelo {
    
    /** Creates new form JP_Cliente */
    public JP_ClienteMasivo() {
        inicializaComponentes();

    }

    private void inicializaComponentes(){
        initComponents();
        //Panel de Operaciones
        ArrayList alBotones = new ArrayList( 1 );
        alBotones.add( new Integer( JP_Utiles_PanelBotonesMantenimiento.BOTON_GUARDAR ) );
        alBotones.add( new Integer( JP_Utiles_PanelBotonesMantenimiento.BOTON_CERRAR ) );

        jp_ope = new JP_Utiles_PanelBotonesMantenimiento( this, alBotones );
        jp_Operaciones.setLayout( new BorderLayout() );
        jp_Operaciones.add( jp_ope, BorderLayout.CENTER );
    }

    private void limpiarForm(){
        this.txtCodigoCatastral.setText("");
        this.txtDireccion.setText("");
        this.txtNombre.setText("");
        this.txtNroInscripcion.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jp_DatosCliente = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtDireccion = new javax.swing.JTextField();
        txtCodigoCatastral = new javax.swing.JTextField();
        txtNroSolicitud = new javax.swing.JTextField();
        txtNroInscripcion = new javax.swing.JFormattedTextField();
        jp_Operaciones = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(controlaccionescomercial.ControlAccionesComercialApp.class).getContext().getResourceMap(JP_ClienteMasivo.class);
        jp_DatosCliente.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jp_DatosCliente.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jp_DatosCliente.border.titleFont"))); // NOI18N
        jp_DatosCliente.setName("jp_DatosCliente"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        txtNombre.setName("txtNombre"); // NOI18N
        txtNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNombreKeyPressed(evt);
            }
        });

        txtDireccion.setName("txtDireccion"); // NOI18N
        txtDireccion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDireccionKeyPressed(evt);
            }
        });

        txtCodigoCatastral.setEditable(false);
        txtCodigoCatastral.setName("txtCodigoCatastral"); // NOI18N

        txtNroSolicitud.setName("txtNroSolicitud"); // NOI18N
        txtNroSolicitud.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNroSolicitudActionPerformed(evt);
            }
        });

        txtNroInscripcion.setName("txtNroInscripcion"); // NOI18N
        txtNroInscripcion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNroInscripcionKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jp_DatosClienteLayout = new javax.swing.GroupLayout(jp_DatosCliente);
        jp_DatosCliente.setLayout(jp_DatosClienteLayout);
        jp_DatosClienteLayout.setHorizontalGroup(
            jp_DatosClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_DatosClienteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jp_DatosClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jp_DatosClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNroSolicitud, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCodigoCatastral, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                    .addComponent(txtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                    .addComponent(txtNroInscripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jp_DatosClienteLayout.setVerticalGroup(
            jp_DatosClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_DatosClienteLayout.createSequentialGroup()
                .addGroup(jp_DatosClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNroInscripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jp_DatosClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jp_DatosClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jp_DatosClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtCodigoCatastral, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jp_DatosClienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtNroSolicitud, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jp_Operaciones.setName("jp_Operaciones"); // NOI18N

        javax.swing.GroupLayout jp_OperacionesLayout = new javax.swing.GroupLayout(jp_Operaciones);
        jp_Operaciones.setLayout(jp_OperacionesLayout);
        jp_OperacionesLayout.setHorizontalGroup(
            jp_OperacionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 415, Short.MAX_VALUE)
        );
        jp_OperacionesLayout.setVerticalGroup(
            jp_OperacionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 37, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jp_DatosCliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jp_Operaciones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jp_DatosCliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jp_Operaciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtNroSolicitudActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNroSolicitudActionPerformed
        // TODO add your handling code here:
        jp_ope.getBtnGuardar().requestFocus();
    }//GEN-LAST:event_txtNroSolicitudActionPerformed

    private void txtNroInscripcionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNroInscripcionKeyPressed
        if( evt.getKeyCode() == evt.VK_ENTER ){
            buscarCliente();
        }
    }//GEN-LAST:event_txtNroInscripcionKeyPressed

    private void txtNombreKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombreKeyPressed
        // TODO add your handling code here:
        if( evt.getKeyCode() == evt.VK_ENTER ){
            this.txtDireccion.requestFocus();
        }
    }//GEN-LAST:event_txtNombreKeyPressed

    private void txtDireccionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDireccionKeyPressed
        // TODO add your handling code here:
         if( evt.getKeyCode() == evt.VK_ENTER ){
             this.txtNroSolicitud.requestFocus();
        }
    }//GEN-LAST:event_txtDireccionKeyPressed

    public void buscarCliente(){
        // TODO add your handling code here:
        String numInscrip = this.txtNroInscripcion.getText().trim();
        tbDatosCliente = null;
        if( numInscrip.trim().equalsIgnoreCase("") ){
            this.txtNombre.requestFocus();            
        }else{
            try {
                tbDatosCliente = GestionDelegate.getInstance().getDatosCliente(numInscrip);
            } catch (Exception ex) {
//                Logger.getLogger(JP_ClienteMasivo.class.getName()).log(Level.SEVERE, null, ex);
                return;
            }
            if( tbDatosCliente == null ){ //Si no encuentra el usuario
                Utiles_MensajesDialogo.mensajeAlerta("El cliente no se encuentra registrado");
                this.txtNombre.requestFocus();
            }else{
                this.txtNombre.setText(tbDatosCliente.getNombre());
                this.txtDireccion.setText(tbDatosCliente.getDireccion());
                this.txtCodigoCatastral.setText(tbDatosCliente.getZona()+ "." + tbDatosCliente.getRegion() + "." + tbDatosCliente.getSector() + "." +
                            tbDatosCliente.getMzn() + "." + tbDatosCliente.getLote() + "." + tbDatosCliente.getSublote());
                this.txtNroSolicitud.requestFocus();
            }
        }
    }

    @Override
    public Task guardar() {
        
        String nroSolicitud = this.txtNroSolicitud.getText().trim();
        String num_inscrip = this.txtNroInscripcion.getText().trim();
        String nombre = this.txtNombre.getText().trim();
        String direccion = this.txtDireccion.getText().trim();
        if( nombre.equalsIgnoreCase("") || direccion.equalsIgnoreCase("")){
            helper.Utiles_MensajesDialogo.mensajeError("CLIENTE INVALIDO: Ingrese datos correctos");
        }else{
            this.tbCliente = new TbCliente();
            tbCliente.setNumInscrip( num_inscrip );
            tbCliente.setNroSolicitud(nroSolicitud);
            tbCliente.setNombre(nombre);
            tbCliente.setDireccion(direccion);
            tbCliente.setNroSolicitud(nroSolicitud);
            JP_Masivo jp_Masivo = (JP_Masivo) this.getRefDep();
            jp_Masivo.agregaCliente(tbCliente);
            jp_Masivo.refrescarTabla();
            this.limpiarForm();
        }
        this.txtNroInscripcion.requestFocus();
        return null;
    } 
   
    @Override
    public Task cerrar() {
        this.cerrarPadre();
        return null;
    }
    
    public TbCliente getTbCliente() {
        return tbCliente;
    }

    public void setTbCliente(TbCliente tbCliente) {
        this.tbCliente = tbCliente;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jp_DatosCliente;
    private javax.swing.JPanel jp_Operaciones;
    private javax.swing.JTextField txtCodigoCatastral;
    private javax.swing.JTextField txtDireccion;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JFormattedTextField txtNroInscripcion;
    private javax.swing.JTextField txtNroSolicitud;
    // End of variables declaration//GEN-END:variables
    private TbCliente tbCliente;
    private JP_Utiles_PanelBotonesMantenimiento jp_ope;
    private TbDatosCliente tbDatosCliente;
}

